//
//  JCUITableViewBinding.m
//  JCSDK
//
//  Created by master on 16/3/17.
//  Copyright © 2016年 AdMaster. All rights reserved.
//

#import "JCUITableViewBinding.h"
#import "JCSwizzler.h"

@implementation JCUITableViewBinding

+ (NSString *)typeName {
    return @"ui_table_view";
}

+ (JCEventBinding *)bindingWithJSONObject:(NSDictionary *)object
{
    NSString *path = object[@"path"];
    if (![path isKindOfClass:[NSString class]] || [path length] < 1) {
        NSLog(@"must supply a view path to bind by");
        return nil;
    }
    
    NSString *eventName = object[@"event_name"];
    if (![eventName isKindOfClass:[NSString class]] || [eventName length] < 1 ) {
        NSLog(@"binding requires an event name");
        return nil;
    }
    
    Class tableDelegate = NSClassFromString(object[@"table_delegate"]);
    if (!tableDelegate) {
        NSLog(@"binding requires a table_delegate class");
        return nil;
    }
    
    return [[JCUITableViewBinding alloc] initWithEventName:eventName
                                                    onPath:path
                                              withDelegate:tableDelegate];
}


- (instancetype)initWithEventName:(NSString *)eventName onPath:(NSString *)path
{
    return [self initWithEventName:eventName onPath:path withDelegate:nil];
}

- (instancetype)initWithEventName:(NSString *)eventName onPath:(NSString *)path withDelegate:(Class)delegateClass
{
    if (self = [super initWithEventName:eventName onPath:path]) {
        [self setSwizzleClass:delegateClass];
    }
    return self;
}


- (void)execute
{
    if (!self.running && self.swizzleClass != nil) {
        void (^block)(id, SEL, id, id) = ^(id view, SEL command, UITableView *tableView, NSIndexPath *indexPath) {
            NSObject *root = [[UIApplication sharedApplication] keyWindow].rootViewController;
            // 判断tableView 是否合法
            if (tableView && [self.path isLeafSelected:tableView fromRoot:root]) {
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                NSString *label = (cell && cell.textLabel && cell.textLabel.text) ? cell.textLabel.text : @"";
                [[self class] track:[self eventName]
                         properties:@{
                                      @"Cell Index": [NSString stringWithFormat: @"%ld", (unsigned long)indexPath.row],
                                      @"Cell Section": [NSString stringWithFormat: @"%ld", (unsigned long)indexPath.section],
                                      @"Cell Label": label
                                      }];
            }
        };
        
        [JCSwizzler swizzleSelector:@selector(tableView:didSelectRowAtIndexPath:)
                            onClass:self.swizzleClass
                          withBlock:block
                              named:self.name];
        self.running = true;
    }
}

- (void)stop
{
    if (self.running && self.swizzleClass != nil) {
        [JCSwizzler unswizzleSelector:@selector(tableView:didSelectRowAtIndexPath:)
                              onClass:self.swizzleClass
                                named:self.name];
        self.running = false;
    }
}


@end
